package com.gwcloud.leetcode.offers;

import com.gwcloud.leetcode.common.datastructure.TreeNode;

/** 
* @ClassName   : Leetcode0681 
* @author      : ganwei 
* @date        : 2020年6月12日 
* @Description : TODO 
*/
public class Leetcode0681 {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
    	if(root == null || root == p || root == q) {
    		return root;
    	}
    	
    	TreeNode left = lowestCommonAncestor(root.left, p, q);
    	TreeNode right = lowestCommonAncestor(root.right, p, q);
    	
    	if(left == null) {
    		return right;
    	}
    	if(right == null) {
    		return left;
    	}
    	return root;
    }
}
